# -*- coding:utf-8 -*-
# @author='dapeng'
# @version='1.0.0'
#https://www.alapi.cn
import urllib.request
import urllib.parse
import json
import configparser
import requests

cf = configparser.ConfigParser()
cf.read("D:/Program Files/pycharm/wechat-robot/configuration/config.ini")  # 读取配置文件，如果写文件的绝对路径，就可以不用os模块

token = cf.get("Alapi", "token")


#早报
def zaobao():
    data = {}
    data['token']=token
    data['format']='json'
    url = 'https://v2.alapi.cn/api/zaobao?'
    url_param=urllib.parse.urlencode(data)
    all_url=url+url_param
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    result = jsonstr.get('data').get('news')
    rep = []
    for item in result:
        rep.append(item)
        rep.append('\r\n')
    return ''.join(rep)


#藏头诗
def poem(keyword):
    data = {}
    data['token']=token
    data['format']='json'
    data['keyword']=keyword
    data['num']=7
    data['type']=1
    url = 'https://v2.alapi.cn/api/ai/poem?'
    url_param=urllib.parse.urlencode(data)
    all_url=url+url_param
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    result = jsonstr.get('data').get('poem')
    return result


#藏短链接
def shorten(lurl):
    data = {}
    data['token']=token
    data['format']='json'
    data['url']=lurl
    data['type']=2
    url = 'https://v2.alapi.cn/api/url?'
    url_param=urllib.parse.urlencode(data)
    all_url=url+url_param
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    result = jsonstr.get('data').get('short_url')
    return result


#每日一文
def mryw():
    data = {}
    data['token']=token
    data['format']='json'
    url = 'https://v2.alapi.cn/api/mryw?'
    url_param=urllib.parse.urlencode(data)
    all_url=url+url_param
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    title = jsonstr.get('data').get('title')
    author = jsonstr.get('data').get('author')
    content = jsonstr.get('data').get('content')
    return title+'\r\n'+author+'\r\n'+content.replace('<p>','').replace('</p>','\r\n')


#每日一文
def mrywrandom():
    data = {}
    data['token']=token
    data['format']='json'
    url = 'https://v2.alapi.cn/api/mryw/random?'
    url_param=urllib.parse.urlencode(data)
    all_url=url+url_param
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    title = jsonstr.get('data').get('title')
    author = jsonstr.get('data').get('author')
    content = jsonstr.get('data').get('content')
    return title+'\r\n'+author+'\r\n'+content.replace('<p>','').replace('</p>','\r\n')


#今日热榜https://tophub.today/  id=KqndgxeLl9 新浪
def hot():
    data = {}
    data['token']=token
    data['id']='mproPpoq6O'
    data['format']='json'
    url = 'https://v2.alapi.cn/api/tophub?'
    url_param=urllib.parse.urlencode(data)
    all_url=url+url_param
    res = requests.get(all_url)
    jsonstr = json.loads(res.text)
    title = jsonstr.get('data').get('name')
    author = jsonstr.get('data').get('last_update')
    content = jsonstr.get('data').get('list')
    contentstr = []
    for c in content:
        contentstr.append(c.get('title'))
        contentstr.append('\r\n')
        contentstr.append(c.get('link').strip())
        contentstr.append('\r\n*****************\r\n')
    return title+'\r\n'+author+'\r\n'+''.join(contentstr)
